<template>
  <div>
        <!-- 面包屑导航区 -->
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-button type="primary" @click="showAddDialog">添加分类</el-button>

        <!-- 表格区域 -->
         <tree-table class="tree-table" :data="cateList" 
         :columns="columns" 
         :selection-type="false" 
         :expand-type="false"
         show-index
         border
         >
         <template slot="isok" slot-scope="scope">
            <i v-if="scope.row.cat_deleted === false" class="el-icon-success" style="color: lightgreen;"></i>
            <i v-else class="el-icon-error"></i>
         </template>
         <template slot="order" slot-scope="scope">
            <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
            <el-tag v-else-if="scope.row.cat_level === 1" size="mini" type="success">二级</el-tag>
            <el-tag v-else size="mini" type="warning">三级</el-tag>
         </template>
         <template slot="opt" slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
         </template>
        </tree-table>
        <!-- 分页 -->
         <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
         </el-pagination>
      </el-card>
      <!-- 添加分类的对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addDialogVisible"
        width="50%"
        @close="addCateDialogClosed"
        >
        <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
            <el-form-item label="分类名称：" prop="cat_name">
                <el-input v-model="addCateForm.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级分类：" >
                <el-cascader
                    v-model="selectedKeys"
                    :options="parentCateList"
                    :props="cascaderProps"
                    clearable
                    @change="parentCateChanged">
                </el-cascader>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
     </el-dialog>
  </div>
</template>

<script>
import { Cascader } from 'element-ui'

export default {
    created() {
        this.getCateList()
        
    },
    data() {
        return {
            queryInfo:{
                type:3,
                pagenum:1,
                pagesize:5
            },
            // 商品分类的数据列表
            cateList:[],
            total:0,
            // 为table指定列的定义
            columns:[
              {
                label:'分类名称',
                prop:'cat_name'
              },
              {
                label:'是否有效',
                // 表示将当前列定义为模版列
                type:'template',
                // 表示当前这一列使用模版名称
                template:'isok'
              },
             {
                label:'排序',
                // 表示将当前列定义为模版列
                type:'template',
                // 表示当前这一列使用模版名称
                template:'order'
             },
             {
                label:'操作',
                // 表示将当前列定义为模版列
                type:'template',
                // 表示当前这一列使用模版名称
                template:'opt'
             }
           ],
           addDialogVisible:false,
           addCateForm:{
            cat_name:'',
            cat_pid:0,
            cat_level:0
           },
           addCateFormRules:{
            cat_name:[
                { required: true, message: '请输入分类名称', trigger: 'blur' }
            ]
           },
           parentCateList:[],
        //    配置级联选择器对象
           cascaderProps:{
            expandTrigger: 'hover' ,
            value:'cat_id',
            label:'cat_name',
            children:'children',
            // checkStrictly:true
            checkStrictly: true
           },
           // 选中父级分类的Id数组
           selectedKeys:[]
           
        }
    },
    methods: {
        async getCateList(){
          const{data:res} = await this.$http.get('categories',{params:this.queryInfo})
          this.cateList = res.data.result
          this.total =res.data.total
        },
        handleSizeChange(newSize){
            this.queryInfo.pagesize= newSize
            this.getCateList()
        },
        handleCurrentChange(newNum){
            this.queryInfo.pagenum = newNum
             this.getCateList()
        },
        showAddDialog(){
            this.getParentCateList()
            this.addDialogVisible = true
        },
        // 获取父级分类的数据列表
        async getParentCateList(){
         const{data : res} = await this.$http.get('categories',{params:{type:2}})
         if(res.meta.status !== 200){
            return this.$message.error('获取父级分类数据失败')
         }
         this.parentCateList = res.data
        },
        // 选择项发生变化触发这个函数
        parentCateChanged(){
            console.log(this.selectedKeys);
            if(this.selectedKeys.length > 0){
                // 父级分类Id
               this.addCateForm.cat_pid =  this.selectedKeys[this.selectedKeys.length -1]

               // 为当前分类赋值
               this.addCateForm.cat_level = this.selectedKeys.length
               return
            }else{
                this.addCateForm.cat_pid = 0
                 this.addCateForm.cat_level = 0 
            }

            
        },
        // 点击按钮，添加分类
        addCate(){
            this.$refs.addCateFormRef.validate(async valid =>{
                if(!valid) return 
             const{data :res} = await this.$http.post('categories',this.addCateForm)
             if(res.meta.status !== 201){
                this.$message.error('添加分类失败')
             }
                this.$message.success('添加分类成功')
                this.getCateList()
                this.addDialogVisible = false
            })
        },
        // 监听对话框的关闭事件
        addCateDialogClosed(){
            this.$refs.addCateFormRef.resetFields()
            this.selectedKeys = []
            this.addCateForm.cat_pid = 0
            this.addCateForm.cat_level =0
        }
        
    },


}
</script>

<style lang="less" scoped>
.tree-table{
    margin-top: 20px;
}

</style>